package com.genesis.yunnanji.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import com.genesis.yunnanji.R;
import com.genesis.yunnanji.adapter.GoodsNewsAdapter;
import com.genesis.yunnanji.adapter.GoodsTitleAdapter;
import com.genesis.yunnanji.bean.GoodsDetailsBean;
import com.genesis.yunnanji.bean.GoodsNewsBean;
import com.genesis.yunnanji.bean.GoodsTitleBean;
import com.genesis.yunnanji.bean.SpecialtyBannerBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;

/**
 * 创建日期：2018/10/19 001914:48
 * 作者：Kite
 * 描述：
 */
public class GoodsDetailsActivity extends BaseActivity {

    @ViewInject(R.id.tb_aboutus_toolbar)
    private Toolbar mToolbar;

    @ViewInject(R.id.textViewTitle)
    private TextView textViewTitle;

    @ViewInject(R.id.textViewContext)
    private TextView textViewContext;

    @ViewInject(R.id.tvTitle)
    private TextView BigTextSize;

    @ViewInject(R.id.textView)
    private TextView textView;

    private Banner goodsBanner;

    private RecyclerView rvLeft;

    private RecyclerView rvRight;

    private List<GoodsDetailsBean.ResultBean.ListBean> mTotalList = new ArrayList<>();

    private List<GoodsTitleBean> mTitleList = new ArrayList<>();

    private GoodsTitleAdapter mGoodsTitleAdapter;//左边适配器

    private GoodsNewsAdapter mGoodsNewsAdapter;//右边适配器

    private List<GoodsNewsBean.ResultBean.ListBean> mNewsList = new ArrayList<>();

    private String mGeneralID = "";//通用ID

    private List<SpecialtyBannerBean.ResultBean.ListBean> bannerList = new ArrayList<>();//后台返回banner图

    private boolean isFirst = true;

    private String cate;

    private String mImgAddress = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState, R.layout.activity_goods_details);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        StatusBarUtil.setColor(this, getResources().getColor(R.color.main_color), 0);
        goodsBanner = findViewById(R.id.goods_banner);
        rvLeft = findViewById(R.id.rv_left);
        rvRight = findViewById(R.id.rv_right);
        Intent intent = getIntent();
        mGeneralID = intent.getStringExtra("id");
        cate = intent.getStringExtra("case");
        BigTextSize.setText(intent.getStringExtra("title"));
        mImgAddress = intent.getStringExtra("img");
        getDetails(mGeneralID);
    }

    @Override
    protected void setUpView() {
        //请求网络
        netBanner();
    }

    private void getDetails(String id) {
        final RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.SPECIALTY_TABNAV);
        params.addBodyParameter("id", id);
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                GoodsDetailsBean bean = gson.fromJson(jsonObject.toString(), GoodsDetailsBean.class);
                mTotalList = bean.getResult().getList();
                rvLeft.setLayoutManager(new LinearLayoutManager(GoodsDetailsActivity.this));
                rvLeft.addItemDecoration(new HorizontalDividerItemDecoration.Builder(GoodsDetailsActivity.this).color(getResources().getColor(R.color.divider)).build());
                mGoodsTitleAdapter = new GoodsTitleAdapter(GoodsDetailsActivity.this, rvLeft, mTitleList);
                rvLeft.setAdapter(mGoodsTitleAdapter);
                initDate();
            }

            @Override
            public void OnCached(JSONObject jsonObject) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

    private void getAboutRead(String nodeID) {
        final RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.PECIALTY_GETLISTTIYAN);
        params.addBodyParameter("speid", mGeneralID);
        params.addBodyParameter("nodeid", nodeID);
        params.addBodyParameter("p", "1");
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                GoodsNewsBean bean = gson.fromJson(jsonObject.toString(), GoodsNewsBean.class);
                mNewsList = bean.getResult().getList();
                if (mNewsList.size() == 0) {
                    textView.setVisibility(View.GONE);
                } else {
                    textView.setVisibility(View.VISIBLE);
                }
                mGoodsNewsAdapter = new GoodsNewsAdapter(GoodsDetailsActivity.this, R.layout.layout_newstext, mNewsList);
                rvRight.setAdapter(mGoodsNewsAdapter);
            }

            @Override
            public void OnCached(JSONObject jsonObject) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

    private void initDate() {
        for (int i = 0; i < mTotalList.size(); i++) {
            GoodsTitleBean goodsTitleBean = new GoodsTitleBean();
            goodsTitleBean.setTitle(mTotalList.get(i).getTitle());
            goodsTitleBean.setContent(mTotalList.get(i).getContent());
            mTitleList.add(goodsTitleBean);
        }
        mGoodsTitleAdapter.notifyDataSetChanged();

        rvRight.setLayoutManager(new LinearLayoutManager(GoodsDetailsActivity.this));
        rvRight.addItemDecoration(new HorizontalDividerItemDecoration.Builder(GoodsDetailsActivity.this).color(getResources().getColor(R.color.divider)).build());

        mGoodsTitleAdapter.setOnItemClickListener(new GoodsTitleAdapter.OnItemClickListener() {//左边recycler点击事件
            @Override
            public void onItemClick(View view, int position) {
                textViewTitle.setText(mTitleList.get(position).getTitle());
                textViewContext.setText(mTitleList.get(position).getContent());
                if (mTitleList.get(position).getContent().equals("")) {
                    textViewTitle.setVisibility(View.GONE);
                    textViewContext.setVisibility(View.GONE);
                } else {
                    textViewTitle.setVisibility(View.VISIBLE);
                    textViewContext.setVisibility(View.VISIBLE);
                }
                mGoodsTitleAdapter.updateCheck(position);
                getAboutRead(mTotalList.get(position).getNode_id());//右边recycler数据请求
            }
        });

        mGoodsTitleAdapter.updateCheck(0);
        if (isFirst) {
            getAboutRead(mTotalList.get(0).getNode_id());
            textViewTitle.setText(mTitleList.get(0).getTitle());
            textViewContext.setText(mTitleList.get(0).getContent());
            if (mTitleList.get(0).getContent().equals("")) {
                textViewTitle.setVisibility(View.GONE);
                textViewContext.setVisibility(View.GONE);
            } else {
                textViewTitle.setVisibility(View.VISIBLE);
                textViewContext.setVisibility(View.VISIBLE);
            }
            isFirst = false;
        }
        mGoodsNewsAdapter.notifyDataSetChanged();
    }

    private void netBanner() {
        final RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.BANNER_ADD);
        if (cate.equals("1")) {
            params.addBodyParameter("code", "app_techan_banner");
        } else if (cate.equals("2")) {
            params.addBodyParameter("code", "app_jun_banner");
        } else {
            params.addBodyParameter("code", "app_feiyi_banner");
        }
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                SpecialtyBannerBean bean = gson.fromJson(jsonObject.toString(), SpecialtyBannerBean.class);
                bannerList = bean.getResult().getList();
                initBanner();
            }

            @Override
            public void OnCached(JSONObject jsonObject) {

            }

            @Override
            public void onFinished() {

            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

    private void initBanner() {

        SpecialtyBannerBean.ResultBean.ListBean bean = new SpecialtyBannerBean.ResultBean.ListBean();
        bean.setAdvert_img(mImgAddress);
        bannerList.add(bean);

        if (bannerList.size() <= 0 || bannerList == null) {
            goodsBanner.setVisibility(View.GONE);
        } else {
            goodsBanner.setVisibility(View.VISIBLE);
            List<String> bannerImages = new ArrayList<>();
            for (int i = 0; i < bannerList.size(); i++) {
                bannerImages.add(bannerList.get(i).getAdvert_img());
            }
            goodsBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            goodsBanner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    ImageOptions imageOptions = new ImageOptions.Builder().setImageScaleType(ImageView.ScaleType.FIT_XY).build();
                    x.image().bind(imageView, GenesisApiConfig.PIC_HOST + (String) path, imageOptions);
                }
            });
//            goodsBanner.setOnBannerListener(new OnBannerListener() {
//                @Override
//                public void OnBannerClick(int position) {
//                    if (bannerList.get(position).getTarget_link().equals("")) {
//                        Toast.makeText(GoodsDetailsActivity.this, "网络链接走丢了!", Toast.LENGTH_SHORT).show();
//                    } else {
//                        Intent intent = new Intent(GoodsDetailsActivity.this, WebActivity.class);
//                        intent.putExtra("url", bannerList.get(position).getTarget_link());
//                        startActivity(intent);
//                    }
//                }
//            });
            goodsBanner.setIndicatorGravity(BannerConfig.CENTER);
            goodsBanner.isAutoPlay(true);
            goodsBanner.setDelayTime(5000);
            goodsBanner.setImages(bannerImages);
            goodsBanner.start();
        }
    }

}
